/**
 * Created by zjl_d on 2017/6/19.
 */
var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbSceneModel = function (obj) {
    ItbBaseModel.call(this, obj);

    // 以下为表定义数据，需要的字段就填入
    //`s_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '场景id',
    //`type` int(3) NOT NULL DEFAULT '0' COMMENT '场景分类: 预留',
    //`title` text COMMENT '标题',
    //`desc` text COMMENT '描述内容',
    //`image` text COMMENT '图片',
    //`url` text COMMENT '链接',
    //`order` int(3) NOT NULL DEFAULT '0' COMMENT '序号',
    //`create_time` datetime NOT NULL,
    //`update_time` datetime NOT NULL,
    //`update_by` varchar(50) DEFAULT NULL COMMENT '修改用户',
    //`del_flg` int(1) NOT NULL DEFAULT '0',
    //PRIMARY KEY (`s_id`)

};
ItbSceneModel.prototype = new ItbBaseModel();

// 取得表名
ItbSceneModel.prototype.getTableName = function () {
    return 'itb_scence';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbSceneModel.prototype.getWhereConditionOfKeyEqual = function () {
    return 's_id=' + this.s_id;
};

// 找到场景案例的列表
ItbSceneModel.prototype.findScenceItemsFromDB = function (callback, pageNum, pageSize) {

    var THIS = this;
    // 从mysql中查询
    pool.pool.getConnection(function (err, connection) {
        var sql = ' SELECT s.*' +
            ' FROM itb_scene s ' +
            ' WHERE s.del_flg=0 ' +
            ' ORDER BY s.order ';

        if (typeof pageNum != 'undefined' && pageNum != null && typeof pageSize != 'undefined' && pageSize != null) {
            var skipCnt = Number(pageNum||'0')*Number(pageSize||'0');
            sql += " limit " + skipCnt + "," + Number(pageSize||'0');
        }

        console.log(sql);

        // Use the connection
        connection.query(sql, function (error, items) {
            // And done with the connection.
            connection.release();

            // 回调
            callback(error, items);
        });
    });
};


//--------------------------------end-------------------------------

// 导出
module.exports = ItbSceneModel;